package com.mwl.gateway.lb;

import com.mwl.gateway.constant.GlobalVariable;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * description:
 *
 * @author maweilong
 **/

@Component
public class LoadBalancerFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 事件处理 exchange 获取请求头
        ServerHttpRequest request=exchange.getRequest();
        // 事件处理 exchange 获取响应头
        ServerHttpResponse response = exchange.getResponse();
        if(request.getHeaders().getFirst(GlobalVariable.GRAY_TAG)!=null){
            // 请求头参数 获取的参数 获取 key 对应的 value值
            response.getHeaders().set(GlobalVariable.GRAY_TAG,request.getQueryParams().getFirst(GlobalVariable.GRAY_TAG));
        }
        // 返回 一个调用链 过滤条件的
        return chain.filter(exchange);
    }
}
